package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.ActionBar;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import app.Disthanitian.thaistyle.EmerMez.EditMyAccount.Runbg;


public class ChangePassword extends Activity{
	
	static UrlEmerMez URLlink = new UrlEmerMez();
	
	public static final String url = URLlink.getUrlchangepass();
	public static String result = "";
	String email;
	MemberUser user = new MemberUser();

	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.changepassword);
		actionBar();
	/*	Button bt_ok = (Button) findViewById(R.id.btnOk);
		Button bt_cancel = (Button) findViewById(R.id.btnCancel);
		bt_ok.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				WebPageTask task = new WebPageTask();
				task.execute();

			}
		});

		bt_cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				finish();

			}
		}); */
		
	}
	
	public void actionBar(){
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("Change Password");
	
		
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.myname, menu);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_ok:
			WebPageTask task = new WebPageTask();
			task.execute();
		
			break;
		case R.id.action_cancel:
			finish();
			
			break;
			
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	
	public String changePass() {
		
		EditText currentPass = (EditText) findViewById(R.id.CurrentPincode);
		EditText newPass = (EditText) findViewById(R.id.NewPincode);
		

		String currentPass1 = currentPass.getText().toString().trim();
		String newPass1 = newPass.getText().toString().trim();
		email = user.getEmail();
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("currentpass", currentPass1));
		nameValuePairs.add(new BasicNameValuePair("newpass", newPass1));
		nameValuePairs.add(new BasicNameValuePair("email", email));
		

		return postData(url, nameValuePairs);

	}
	

	public String postData(String url, List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}
	
	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
		
			result = changePass();
			return result;
		}

		protected void onPostExecute(String result) {
			if (result.trim().equals("ok")) {
				Toast.makeText(ChangePassword.this,
						"Change Password Success !!",
						Toast.LENGTH_LONG).show();
			} else if (result.trim().equals("fail")) {
				Toast.makeText(ChangePassword.this, "Current Password Invalid !!",
						Toast.LENGTH_LONG).show();
			} 

		}

	}
}
